package com.huosu.utils;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author ：liyingchun
 * @date ：Created in 2019/7/4 0004 15:05
 * @description：字符串工具类
 * @modified By：
 */
public class StringUtil {

    private static Pattern NUMBER_PATTERN = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");

    private static String randString = "34678ABCDEFHJKLMNPRTUVWXY";//随机产生数字与字母组合的字符串

    /**
     * 获取随机的字符
     */
    public static String getRandomUuid() {
        String randomString = "";
        Random random = new Random();
        for (int i = 1; i <= 8; i++) {
            String rand = String.valueOf(getRandomString(random.nextInt(randString
                    .length())));

            randomString += rand;
        }
        String time = DateUtil.getTimeStrWithoutFormat().substring(10);
        randomString += time;
        return randomString;
    }

    /**
     * 获取随机的字符
     */
    public static String getRandomString(int num) {
        return String.valueOf(randString.charAt(num));
    }

    private StringUtil() {
        // to do nothing
    }

    /**
     * 判断字符串是否为空
     *
     * @param cs
     * @return
     */
    public static boolean isEmpty(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串是否不为空
     *
     * @param cs
     * @return
     */
    public static boolean isNotEmpty(final CharSequence cs) {
        return !isEmpty(cs);
    }

    /**
     * 利用正则表达式来判断字符串是否为数字
     */
    public static boolean checkStrIsNum(String str) {
        Matcher isNum = NUMBER_PATTERN.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
     * List转字符串拼接
     *
     * @param list
     * @param separator
     * @return
     */
    public static String listToString(List list, char separator) {
        return StringUtils.join(list.toArray(), separator);
    }

    /**
     * 首字母大写
     *
     * @param str
     * @return
     */
    public static String firstUpperCase(String str) {
        char[] ch = str.toCharArray();
        if (ch[0] >= 'a' && ch[0] <= 'z') {
            ch[0] = (char) (ch[0] - 32);
        }
        return new String(ch);
    }

    /*
     * 转换单位
     */
    public static String hashRateUnit(String difficulty) {
        if (StringUtil.isNotEmpty(difficulty)) {
            BigDecimal bd_difficulty = new BigDecimal(difficulty);
            BigDecimal bd_p = new BigDecimal(1000);
            int i = 0;
            String[] units = {"MH/s", "GH/s", "TH/s", "PH/s", "EH/s"};
            while (bd_difficulty.compareTo(bd_p) > 0 && i < 5) {
                bd_difficulty = bd_difficulty.divide(bd_p, 8, RoundingMode.HALF_UP);
                i++;
            }
            return bd_difficulty.setScale(2, RoundingMode.HALF_UP).stripTrailingZeros().toPlainString() + " " + units[i];
        } else {
            return "0 MH/s";
        }
    }

    /*
     * 转换单位
     */
    public static String hashRateToThs(String hash) {
        if (StringUtil.isNotEmpty(hash)) {
            BigDecimal bd_hash = new BigDecimal(hash);
            BigDecimal bd_p = new BigDecimal(1000000);
            bd_hash = bd_hash.divide(bd_p, 6, RoundingMode.HALF_UP);
            return bd_hash.stripTrailingZeros().toPlainString();
        } else {
            return "0";
        }
    }

    /**
     * 获取域名
     *
     * @param address
     * @return
     */
    public static String getDomain(String address) {
        String domain;
        try {
            //域名为空，设置域名为地址“:”前面的字符
            domain = address.split(":")[0];
            if (IpUtil.isIp(domain)) {
                //“:”前面的字符是IP
                return domain;
            } else {
                //校验domain包含几个.
                String[] str = domain.split("\\.");
                if (str.length < 2) {
                    return domain;
                } else if (str.length == 2) {
                    return domain;
                } else {
                    domain = domain.substring(domain.lastIndexOf(".", domain.lastIndexOf(".") - 1) + 1);
                    return domain;
                }
            }
        } catch (Exception e) {
            domain = "";
        }

        return domain;
    }

}
